<template>
    <div>
        <el-button type="danger" @click="abortSubject">终止课题</el-button>
        <el-button type="primary" @click="completeSubject">结束课题</el-button>
    </div>
</template>

<script setup lang="ts">
import useSubjectStore from "@/store/modules/subject";
import useLayOutSettingStore from "@/store/modules/setting";
import { ElMessage, ElMessageBox } from "element-plus";
// 引入接口
import { reqAbortSubject, reqCompleteSubject } from "@/api/subject";
const subjectStore = useSubjectStore();
const layOutSettingStore = useLayOutSettingStore();
// 定义方法
const abortSubject = async () => {
    try {
        await ElMessageBox.confirm("确认终止该课题吗，此后无法对该课题进行任何操作", {
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            type: "warning",
        });
    } catch (error) {
        return;
    }

    const result = await reqAbortSubject({
        id: +subjectStore.currentId,
    });
    if (result.code === 200) {
        ElMessage.success("终止课题成功");
        // 刷新
        layOutSettingStore.refresh = !layOutSettingStore.refresh;
    } else {
        ElMessage.error(result.data);
    }
};

// 定义方法
const completeSubject = async () => {
    try {
        await ElMessageBox.confirm("确认结题吗，此后无法对该课题进行任何操作", {
            confirmButtonText: "确认",
            cancelButtonText: "取消",
            type: "warning",
        });
    } catch (error) {
        return;
    }
    // 首先确认是否完成了进度
    if (subjectStore.currentInfo.schedule !== 4) {
        ElMessage.error("请先完成前置任务后再结题");
        return;
    }
    const result = await reqCompleteSubject({
        id: +subjectStore.currentId,
    });
    if (result.code === 200) {
        ElMessage.success("终止结题成功");
        // 刷新
        layOutSettingStore.refresh = !layOutSettingStore.refresh;
    } else {
        ElMessage.error(result.data);
    }
};
</script>

<style lang="scss" scoped></style>
